﻿namespace BullsEyeGame
{
    internal class Guess
    {
        private string m_GuessString = null;
        public const int k_GuessLength = 4;
        public const string k_ValidChars = "ABCDEFGH";
        public const int k_CharNotFound = -1;

        private Guess(string i_StingGuess)
        {
            m_GuessString = i_StingGuess;
        }

        public static char GetValidCharsAt(int i_CharIndex)
        {
            return k_ValidChars[i_CharIndex];
        }

        public static Guess CreateGuess(string i_NewGuessString)
        {
            bool isValidGuess = (i_NewGuessString != null) && (i_NewGuessString.Length == k_GuessLength);
            int i = 0;

            while ((i < i_NewGuessString.Length) && isValidGuess)
            {
                isValidGuess = k_ValidChars.IndexOf(i_NewGuessString[i]) != k_CharNotFound;
                i++;
            }

            return isValidGuess ? new Guess(i_NewGuessString) : null;
        }

        public char At(int index)
        {
            return m_GuessString[index];
        }

        public int? IndexOf(char i_CharToFindInGuess, int i_StartSearchIndex)
        {            
            int? result = null;

            if ((0 <= i_StartSearchIndex) && (i_StartSearchIndex <= m_GuessString.Length))
            {
                result = m_GuessString.IndexOf(i_CharToFindInGuess, i_StartSearchIndex);
            }

            return result; 
        }

    }
}
